﻿using AutoMapper;
using ManagementApi.Authorizers;
using ManagementApi.Models;
using ManagementApi.Models.Project;
using ManagementApi.Mvc;
using ManagementApi.Mvc.ApplicationContexts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Person;
using Modules.Project;
using Modules.SMS;
using PmSoft.Entity;
using PmSoft.Exceptions;
using static Modules.Project.ProjectConstants;

namespace ManagementApi.Controllers.ConstructorPannel
{
    /// <summary>
    /// 建设单位相关业务接口
    /// </summary>
    [Route("/api/v1/constructor/project")]
    [ApiController]
    [Authorize(Roles = "Constructor")]
    public class ProjectController : ControllerBase
    {
        private readonly ProjectService projectService;
        private readonly IMapper mapper;
        private readonly ConstructorApplicationContext applicationContext;
        private readonly SmsSendLogService smsSendLogService;
        private readonly ProjectUpdateApplyService projectUpdateApplyService;
        private readonly PersonService personService;
        private readonly ProjectCorpService projectCorpService;

        public ProjectController(ProjectService projectService, IMapper mapper, ConstructorApplicationContext applicationContext, SmsSendLogService smsSendLogService, PersonService personService, ProjectUpdateApplyService projectUpdateApplyService, ProjectCorpService projectCorpService)
        {
            this.projectService = projectService;
            this.mapper = mapper;
            this.applicationContext = applicationContext;
            this.smsSendLogService = smsSendLogService;
            this.personService = personService;
            this.projectUpdateApplyService = projectUpdateApplyService;
            this.projectCorpService = projectCorpService;
        }

        #region 项目

        /// <summary>
        /// author:hzj date:2023/12/08 describe:项目分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("project_page")]
        public ApiResult<PageDto<ProjectInfoDto>> GetProjects([FromQuery] ProjectInfoQuery query, int pageIndex = 1, int pageSize = 20)
        {
            query.BuildCorpId = applicationContext.CurrentConstructor.ConstructorId;
            var pageDto = projectService.GetProjectInfos(query, pageIndex, pageSize).AsPageDto(m => m.AsDto(applicationContext));
            return ApiResult<PageDto<ProjectInfoDto>>.Success(pageDto);
        }

        /// <summary>
        /// author:hzj date:2023/12/08 describe:获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("base_info/{id}")]
        public ApiResult<ProjectBaseInfo> GetProjectBaseInfo(int id)
        {
            var info = projectService.GetProjectInfo(id);
            if (info == null) return ApiResult<ProjectBaseInfo>.Failure("不存在该项目");
            return ApiResult<ProjectBaseInfo>.Success(info.AsBaseInfo(applicationContext));
        }

        /// <summary>
        /// author:hzj date:2023/12/08 describe:获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        public ApiResult<ProjectInfoDto> GetProjectInfo(int id)
        {
            var info = projectService.GetProjectInfo(id);
            if (info == null) return ApiResult<ProjectInfoDto>.Failure("不存在该项目");
            ProjectInfoDto projectInfoDto = info.AsDto(applicationContext);
            return ApiResult<ProjectInfoDto>.Success(projectInfoDto);
        }

        /// <summary>
        /// author:hzj date:2023/12/08 describe:项目表单数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/projectform")]
        public ApiResult<ProjectRegisterForm> GetProject(int id)
        {
            var data = projectService.GetProjectInfo(id);
            if (data == null)
                return ApiResult<ProjectRegisterForm>.Failure("项目数据不存在");
            return ApiResult<ProjectRegisterForm>.Success(data.AsForm(applicationContext));
        }

        /// <summary>
        /// author:hzj date:2023/12/08 describe:项目下拉
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("pulldown")]
        public ApiResult<PageDto<LabelValue<int>>> GetPulldown([FromQuery] ProjectInfoQuery query, int pageIndex = 1, int pageSize = 9999)
        {
            query.BuildCorpId = applicationContext.CurrentConstructor.ConstructorId;
            query.AuditStatus = 项目审核状态.通过;
            var pageDto = projectService.GetProjectInfosByManagerId(query, pageIndex, pageSize);
            var dtos = pageDto.AsPageDto(source => { return source.AsPullDownDto(); });
            return ApiResult<PageDto<LabelValue<int>>>.Success(dtos);
        }

        /// <summary>
        /// 修改申请分页
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("update_apply_page")]
        public ApiResult<PageDto<ProjectUpdateApplyPageDto>> GetProjects([FromQuery] ProjectUpdateApplyQuery query, int pageIndex = 1, int pageSize = 20)
        {
            query.ProjectId = applicationContext.CurrentProjectInfo.ProjectId;
            var pageDto = projectUpdateApplyService.GetProjectUpdateApplys(query, pageIndex, pageSize).AsPageDto(m => m.AsPageDto());
            return ApiResult<PageDto<ProjectUpdateApplyPageDto>>.Success(pageDto);
        }

        /// <summary>
        /// 获取本月未审核的修改申请
        /// </summary>
        /// <returns></returns>
        [HttpGet("exists_update_apply/{projectId}")]
        public ApiResult<bool> ExistsByPrjIdNoAudit(int projectId)
        {
            return ApiResult<bool>.Success(projectUpdateApplyService.ExistsByPrjIdNoAudit(projectId));
        }

        /// <summary>
        /// 获取修改申请信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("update_apply_info/{projectId}")]
        public ApiResult<AuditProjectUpdateApplyDto> GetUpdateProjectApply(int projectId)
        {
            AuditProjectUpdateApplyDto dto = new();
            dto.NewInfo = projectUpdateApplyService.GetProjectUpdateApplyByProjectId(projectId);
            dto.OldInfo = projectService.GetProjectInfo(projectId);
            return ApiResult<AuditProjectUpdateApplyDto>.Success(dto);
        }

        /// <summary>
        /// 修改申请
        /// </summary>
        /// <returns></returns>
        [HttpPut("update_apply_audit/{projectId}")]
        public ApiResult AuditUpdateProjectApply(int projectId, AuditUpdateApplyForm form)
        {
            #region 验证数据权限

            if (applicationContext.CurrentUser == null) throw new BusinessException("登录信息不存在");
            var projects = projectService.GetProjectInfosByConstructorId(applicationContext.CurrentUser.Id);
            if (!projects.Any(s => s.ProjectId == projectId)) throw new OwnAuthorizationFailedException();

            #endregion 验证数据权限

            var projectUpdateApply = projectUpdateApplyService.GetProjectUpdateApplyByProjectId(projectId);
            if (projectUpdateApply == null)
            {
                return ApiResult.Failure("该项目无修改申请,不必审核");
            }
            projectUpdateApply.AuditType = form.AuditType;
            if (form.AuditType == 项目修改申请状态.未通过)
            {
                projectUpdateApplyService.Update(projectUpdateApply);
                return ApiResult.Success("操作成功");
            }
            var projectInfo = projectService.GetProjectInfo(projectUpdateApply.ProjectId);
            if (projectInfo == null) return ApiResult.Failure("项目不存在");
            var updateForm = projectUpdateApply.AsForm();
            mapper.Map(updateForm, projectInfo);
            projectUpdateApplyService.Audit(projectUpdateApply, projectInfo, updateForm.Attachments);
            return ApiResult.Success("申请成功");
        }

        #endregion 项目
    }
}